#!/usr/bin/python
# Filename: ex_DSintro_work_1_2.py

# Complex Number and Fraction

def greatest_commen_divisor(x, y):
    if x == 0 and y != 0:
        return (abs(y))
    elif x != 0 and y == 0:
        return (abs(x))
    elif x == 0 and y == 0:
        return (0)

    x = abs(x)
    y = abs(y)

    if x < y:
        x, y = y, x
    while y != 1:
        z = x % y
        if z == 0:
            return (y)
        x, y = y, z
    return (1)


class ComplexNumber:

    def __init__(self, real=0, imaginary=0):
        self.real = real
        self.imaginary = imaginary


class Fraction:

    def __init__(self, numerator, denominator=1):
        sign = 1
        if numerator * denominator < 0:
            sign = -1
        numerator = abs(numerator)
        denominator = abs(denominator)
        gcd = greatest_commen_divisor(numerator, denominator)
        self.numerator = sign * numerator / gcd
        self.denominator = denominator / gcd

    def __str__(self):
        return "%d/%d" % (self.numerator, self.denominator)

    def __mul__(self, other):
        if type(other) == type(0):
            other = Fraction(other)
        numerator = self.numerator * other.numerator
        denominator = self.denominator * other.denominator
        mcf = greatest_commen_divisor(numerator, denominator)
        return (Fraction(numerator/mcf, denominator/mcf))

    def __add__(self, other):
        if type(other) == type(0):
            other = Fraction(other)
        numerator = self.numerator*other.denominator + self.denominator*other.numerator
        denominator = self.denominator * other.denominator
        mcf = greatest_commen_divisor(numerator, denominator)
        return (Fraction(numerator/mcf, denominator/mcf))

    __rmul__ = __mul__

    def __cmp__(self, other):
        if type(other) == type(0):
            other = Fraction(other)
        diff = (self.numerator*other.denominator - self.denominator*other.numerator)
        return (diff)

    __rcmp__ = __cmp__

    def __neg__(self):
        return (Fraction(-1*self.numerator, self.denominator))

    def __sub__(self, other):
        return (self + (-other))

if __name__ == "__main__":
    print "MCF(2, 3) =", greatest_commen_divisor(2, 3)
    print "MCF(10, 100) =", greatest_commen_divisor(10, 100)
    print "MCF(12, 15) =", greatest_commen_divisor(12, 15)
    print "MCF(-12, -15) =", greatest_commen_divisor(-12, -15)
    print "MCF(-12, 15) =", greatest_commen_divisor(-12, 15)
    a = Fraction(2, 5)
    b = Fraction(15, 62)
    c = a * b
    print "%s * %s = %s" % (a, b, c)
    d = a + 3
    print "%s + 3 = %s" % (a, d)
    e = -2 * b
    print "-2 * %s = %s" % (b, e)
    print "1 > %s ? :" % (a,) , 1 > a
    print "%s > 3/4 ? :" % (b,) , b > Fraction(3, 4)
    print "%s = -15/31 ? :" %(e,), e == Fraction(-15, 31)
    f = -e
    print "-%s = %s" % (e, f)
    g = a - b
    print "%s - %s = %s" % (a, b, g)
